import { Injectable } from '@nestjs/common';
import { TypeOrmOptionsFactory, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { ConfigService } from '@/common/config/config.service';
import { User } from '@/modules/users';
import {
  Artist,
  Album,
  Song,
  ArtistEdition,
  AlbumEdition,
  ResourceFile,
  PlayList,
  Score,
  Track,
  Post,
} from '@/modules/resources';
import { Captcha } from '@/common/captcha/captcha.entity';

@Injectable()
export class TypeOrmConfig implements TypeOrmOptionsFactory {
  constructor(private readonly config: ConfigService) {}

  createTypeOrmOptions(): TypeOrmModuleOptions {
    return {
      type: 'postgres',
      host: this.config.pgHost,
      port: this.config.pgPort,
      username: this.config.pgUsername,
      password: this.config.pgPassword,
      database: this.config.pgDatabase,
      schema: this.config.pgSchema,
      entities: [
        User,
        Artist,
        ArtistEdition,
        Album,
        AlbumEdition,
        Song,
        ResourceFile,
        PlayList,
        Score,
        Track,
        Post,
        Captcha,
      ],
      // synchronize: this.config.nodeEnv !== 'production',
      synchronize: false,
      logging: true,
      cache: true,
    };
  }
}
